package states;

import interfaces.StateController;

import java.util.ArrayList;

import objects.*;
import ect.GeneralPause;
import globals.*;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import buttons.CircleToggleButton;
import buttons.ClickButton;
import buttons.StateLight;

public class Blank extends BasicGameState {
	//control
	StateController sController;
	public Input input; // key imput
	StateLight sl0,sl1,sl2,slLs;
	GeneralPause gPause;
	//module 1
	Vector2D a,b,c;
	Vector2D a45,b45,c45,d45;
	int centerX,centerY;
	int rad=1;
	// module 2
	Vector2D aM2,bM2,cM2;
	int functionState=0; // 0 add, 1 sub, 2 dot
	int gridOriginX,gridOriginY;
	int gridLength, gridHeight;
	CircleSlider aSlider, bSlider;

	
	private int id;
	Blank(int stateId){
		
		//control
		this.id=stateId;
		sController=new StateController();
		sController.setDelay(3*SlickGame.fps);
		sController.key_1=true;
		sl0=new StateLight(15, SlickGame.height-20, 5, new Color(0,148,255), new Color(0,22,38), Color.black.brighter());
		sl1=new StateLight(27, SlickGame.height-20, 5, new Color(0,148,255), new Color(0,22,38), Color.black.brighter());
		sl2=new StateLight(39, SlickGame.height-20, 5, new Color(0,148,255), new Color(0,22,38), Color.black.brighter());
		slLs=new StateLight(51, SlickGame.height-20, 5, new Color(0,148,255), new Color(0,22,38), Color.black.brighter());
		gPause=new GeneralPause("Blank",id);
		gPause.isPaused=true;
		
		//module 1
		centerX=SlickGame.width/2;
		centerY=SlickGame.height/2;
		rad=225;
		
		a=new Vector2D(3,3);
		b=new Vector2D(3,4);
		c=new Vector2D(5,12);
		c.setMagnatude(rad);
		
		a45=new Vector2D(1,1);
		b45=new Vector2D(-1,1);
		c45=new Vector2D(-1,-1);
		d45=new Vector2D(1,-1);
		a45.setMagnatude(rad);
		b45.setMagnatude(rad);
		c45.setMagnatude(rad);
		d45.setMagnatude(rad);
		
		//module 2
		aM2=new Vector2D(10,0);
		bM2=new Vector2D(0,10);
		cM2=new Vector2D();
		aSlider=new CircleSlider(300,300,7);
		bSlider=new CircleSlider(350,350,7);
		gridOriginX=150;
		gridOriginY=SlickGame.height-150;
		gridLength=400;
		gridHeight=305;
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		gPause.init(gc, sbg);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setBackground(Color.black);
		g.setColor(Color.white);
		
		
		if(sController.key_1){
			this.drawModule1(gc, sbg, g);
		}
		
		if(sController.key_2){
			this.drawModule2(gc, sbg, g);
		}
		
		sl0.draw(g);
		sl1.draw(g);
		sl2.draw(g);
		slLs.draw(g);
		g.setColor(Color.gray.darker());
		g.drawString("Blank",SlickGame.width/2-20,20);
		g.drawString("Mouse: ["+Mouse.getX()+" "+(SlickGame.height-Mouse.getY())+"] ("+Mouse.isButtonDown(0)+")", SlickGame.width/2-75, SlickGame.height-25);
		
		if(gPause.isPaused){
			gPause.draw(g);
		}
	}
	
	public void drawModule1(GameContainer gc, StateBasedGame sbg, Graphics g){
		g.setColor(Color.gray.darker().darker());
		int i=100; // iterated y val
		int k=50; // static x val.
		g.drawString("A:  ["+a.getX()+" "+a.getY()+"]: "+a.getAngle()+"  "+a.getMagnatude(), k, i);	i+=15;
		g.drawString("B:  ["+b.getX()+" "+b.getY()+"]: "+b.getAngle()+"  "+b.getMagnatude(), k, i);	i+=15;
		g.drawString("C:  ["+c.getX()+" "+c.getY()+"]: "+c.getAngle()+"  "+c.getMagnatude(), k, i);	i+=15;
		g.drawString("", 200, i);	i+=15;
		g.drawString("[1,0]:"+	(new Vector2D(1,0)).getAngle()+" "+(new Vector2D(1,0)).getMagnatude(), k, i);	i+=15;
		g.drawString("[1,1]:"+	(new Vector2D(1,1)).getAngle()+" "+(new Vector2D(-1,1)).getMagnatude(), k, i);	i+=15;
		g.drawString("[0,1]:"+	(new Vector2D(0,1)).getAngle()+" "+(new Vector2D(0,1)).getMagnatude(), k, i);	i+=15;
		g.drawString("[-1,1]:"+	(new Vector2D(-1,1)).getAngle()+" "+(new Vector2D(-1,1)).getMagnatude(), k, i);	i+=15;
		g.drawString("[-1,0]:"+	(new Vector2D(-1,0)).getAngle()+" "+(new Vector2D(-1,0)).getMagnatude(), k, i);	i+=15;
		g.drawString("[-1,-1]:"+(new Vector2D(-1,-1)).getAngle()+" "+(new Vector2D(-1,-1)).getMagnatude(), k, i);	i+=15;
		g.drawString("[0,-1]:"+	(new Vector2D(0,-1)).getAngle()+" "+(new Vector2D(0,-1)).getMagnatude(), k, i);	i+=15;
		g.drawString("[1,-1]:"+	(new Vector2D(1,-1)).getAngle()+" "+(new Vector2D(1,-1)).getMagnatude(), k, i);	i+=15;
		g.drawString("", k, i);	i+=15;
		g.drawString("Sqrt(2)="+Math.sqrt(2), k, i);	i+=15;
		g.drawString("", k, i);	i+=15;
		g.drawString("atan 1 1: "+Math.atan2(1, 1)+" :: "+(Math.atan2(1, 1)+Math.PI), k, i);	i+=15;
		g.drawString("atan 1 -1: "+Math.atan2(1, -1)+" :: "+(Math.atan2(1, -1)+Math.PI), k, i);	i+=15;
		g.drawString("atan -1 -1: "+Math.atan2(-1, -1)+" :: "+(Math.atan2(-1, -1)+2*Math.PI), k, i);	i+=15;
		g.drawString("atan -1 1: "+Math.atan2(-1, 1)+" :: "+(Math.atan2(-1, 1)+2*Math.PI), k, i);	i+=15;
		g.drawString("", k, i);	i+=15;
		
		k=SlickGame.height+rad+50;
		i=100;
		g.drawString("1 1:\t"+(new Vector2D(1,1).getAngle()), k, i);	i+=15;
		g.drawString("-1 1:\t"+(new Vector2D(-1,1).getAngle()), k, i);	i+=15;
		g.drawString("-1 -1:\t"+(new Vector2D(-1,-1).getAngle()), k, i);	i+=15;
		g.drawString("1 -1:\t"+(new Vector2D(1,-1).getAngle()), k, i);	i+=15;
		g.drawString("", k, i);	i+=15;
		g.setColor(Color.lightGray);
		g.drawString("Radius of Unit Circle: "+rad, k, i);	i+=15;
		g.drawString("Center: ("+centerX+", "+centerY+")", k, i);	i+=15;
		Vector2D unitV=c.getUnitVector();
		g.drawString("Vector:["+unitV.getX()+" "+unitV.getY()+"]:", k, i);	i+=15;
		g.drawString("   Angle: "+unitV.getAngle(), k, i);	i+=15;
		g.drawString("   Magnatude: "+unitV.getMagnatude(), k, i);	i+=15;
		g.drawString("", k, i);	i+=15;
		g.setColor(Color.darkGray);
		g.drawString("Pi/2=  "+(Math.PI/2), k, i);	i+=15;
		g.drawString("Pi/3=  "+(Math.PI/3), k, i);	i+=15;
		g.drawString("Pi/4=  "+(Math.PI/4), k, i);	i+=15;
		g.drawString("Pi/6=  "+(Math.PI/6), k, i);	i+=15;
		
		g.setColor(Color.lightGray);
		g.draw(new Line(centerX,centerY,centerX,centerY+rad));
		g.draw(new Line(centerX,centerY,centerX-rad,centerY));
		g.draw(new Line(centerX,centerY,centerX,centerY-rad));
		g.setColor(Color.lightGray.darker());
		g.draw(new Line(centerX,centerY,centerX+a45.getX(),centerY+a45.getY()));
		g.draw(new Line(centerX,centerY,centerX+b45.getX(),centerY+b45.getY()));
		g.draw(new Line(centerX,centerY,centerX+c45.getX(),centerY+c45.getY()));
		g.draw(new Line(centerX,centerY,centerX+d45.getX(),centerY+d45.getY()));
		
		g.setColor(Color.white);
		g.draw(new Line(centerX,centerY,centerX+rad,centerY));
		g.drawString("Q1", centerX+rad/2-20, centerY+rad/2);
		g.drawString("Q2", centerX-rad/2, centerY+rad/2);
		g.drawString("Q3", centerX-rad/2, centerY-rad/2);
		g.drawString("Q4", centerX+rad/2-20, centerY-rad/2);
		g.draw(new Circle(centerX,centerY,3));
		g.draw(new Circle(centerX,centerY,rad));
		g.draw(new Circle(centerX,centerY,rad+3));
		
		g.setColor(Color.green);
		if(c.getAngle()==0 || Math.abs(c.getAngle()-Math.PI/2)<0.002 || Math.abs(c.getAngle()-Math.PI)<0.002 || Math.abs(c.getAngle()-(3*Math.PI/2))<0.002){
			g.setColor(new Color(226, 189, 0));
		}
		if(Math.abs(c.getAngle()-Math.PI/3)<0.002 || Math.abs(c.getAngle()-2*Math.PI/3)<0.002 || Math.abs(c.getAngle()-4*Math.PI/3)<0.002 || Math.abs(c.getAngle()-5*Math.PI/3)<0.002){
			g.setColor(Color.blue.brighter());
		}
		if(Math.abs(c.getAngle()-Math.PI/6)<0.002 || Math.abs(c.getAngle()-5*Math.PI/6)<0.002 || Math.abs(c.getAngle()-7*Math.PI/6)<0.002 || Math.abs(c.getAngle()-11*Math.PI/6)<0.002){
			g.setColor(Color.blue.brighter());
		}
		if(Math.abs(c.getAngle()-Math.PI/4)<0.002 || Math.abs(c.getAngle()-3*Math.PI/4)<0.002 || Math.abs(c.getAngle()-5*Math.PI/4)<0.002 || Math.abs(c.getAngle()-7*Math.PI/4)<0.002){
			g.setColor(Color.blue.brighter());
		}
		g.draw(new Line(centerX,centerY,centerX+c.getX(),centerY+c.getY()));
		
		g.setColor(Color.gray.darker());
		g.drawString("MouseQ: ["+(Mouse.getX()-centerX)+" "+((SlickGame.height-Mouse.getY())-centerY)+"]", SlickGame.width/2-75, SlickGame.height/2-30-rad);
	}
	
	public void drawModule2(GameContainer gc, StateBasedGame sbg, Graphics g){
		g.setColor(Color.gray.darker().darker());
		int itrY=100; // iterated y val
		int itrX=700; // static x val.
		g.drawString("", itrX, itrY);	itrY+=15;
		g.drawString("Red Vector ["+(aM2.getX())+" "+(aM2.getY())+"]", itrX, itrY);	itrY+=15;
		g.drawString("           Angle: "+aM2.getAngle(), itrX, itrY);	itrY+=15;
		g.drawString("           Mag: "+aM2.getMagnatude(), itrX, itrY);	itrY+=15;
		g.drawString("", itrX, itrY);	itrY+=15;
		g.drawString("Green Vector ["+(bM2.getX())+" "+(bM2.getY())+"]", itrX, itrY);	itrY+=15;
		g.drawString("             Angle: "+bM2.getAngle(), itrX, itrY);	itrY+=15;
		g.drawString("             Mag: "+bM2.getMagnatude(), itrX, itrY);	itrY+=15;
		g.drawString("", itrX, itrY);	itrY+=15;
		g.drawString("Blue Vector ["+(cM2.getX())+" "+(cM2.getY())+"]", itrX, itrY);	itrY+=15;
		g.drawString("(Red+Green) Angle: "+cM2.getAngle(), itrX, itrY);	itrY+=15;
		g.drawString("            Mag: "+cM2.getMagnatude(), itrX, itrY);	itrY+=15;
		
		
		g.setColor(Color.darkGray);
		for(int i=0; i<=gridHeight; i+=10){
			g.draw(new Line(gridOriginX,gridOriginY-i,gridOriginX+gridLength,gridOriginY-i));
		}
		for(int i=0; i<=gridLength; i+=10){
			g.draw(new Line(gridOriginX+i,gridOriginY,gridOriginX+i,gridOriginY-gridHeight));
		}
		g.setColor(Color.white);
		g.draw(new Line(gridOriginX-10,gridOriginY,gridOriginX+gridLength,gridOriginY));
		g.draw(new Line(gridOriginX,gridOriginY+10,gridOriginX,gridOriginY-gridHeight));
		
		g.setColor(Color.red);
		g.draw(new Line(gridOriginX,gridOriginY,gridOriginX+aM2.getX(),gridOriginY+aM2.getY()));
		g.setColor(Color.green);
		g.draw(new Line(gridOriginX,gridOriginY,gridOriginX+bM2.getX(),gridOriginY+bM2.getY()));
		g.setColor(Color.green.darker().darker());
		g.draw(new Line(gridOriginX+aM2.getX(),gridOriginY+aM2.getY(),gridOriginX+aM2.getX()+bM2.getX(),gridOriginY+aM2.getY()+bM2.getY()));
		g.setColor(Color.blue);
		g.draw(new Line(gridOriginX,gridOriginY,gridOriginX+cM2.getX(),gridOriginY+cM2.getY()));
		
		aSlider.draw(g);
		bSlider.draw(g);
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		this.input = gc.getInput();
		sController.update(input, delta);
		sl0.update(sController.key_0);
		sl1.update(sController.key_1);
		sl2.update(sController.key_2);
		slLs.update(sController.key_lShift);
		
		if(!gPause.isPaused){
			if(sController.key_1){
				if(Mouse.isButtonDown(0)){
					//c.setPoint((Mouse.getX()-centerX), ((SlickGame.height-Mouse.getY())-centerY));
					c.setX((Mouse.getX()-centerX));
					c.setY(((SlickGame.height-Mouse.getY())-centerY));
				}
				c.setMagnatude(rad);
			}
			if(sController.key_2){
				if(Mouse.getX()>=gridOriginX && !(Mouse.getX()>gridOriginX+gridLength) && ((SlickGame.height-Mouse.getY())>=gridOriginY-gridHeight && !((SlickGame.height-Mouse.getY())>gridOriginY))){
					aSlider.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0)&&sController.key_0);
					bSlider.update(Mouse.getX(), SlickGame.height-Mouse.getY(), Mouse.isButtonDown(0)&&!sController.key_0);
				}
				aM2.setPoint(aSlider.x-gridOriginX, aSlider.y-gridOriginY);
				bM2.setPoint(bSlider.x-gridOriginX, bSlider.y-gridOriginY);
				cM2=aM2.getCopy();
				cM2.add(bM2);
				
			}
		}
		else{
			gPause.update(sbg, delta);
		}
		
		if(sController.key_lShift){
			sController.key_lShift=false;
			gPause.pauseState=gPause.playState;
			gPause.togglePause();
		}
	}
	
	public static void destroy(){
		
	}

	public int getID() {
		return this.id;
	}


}
